package com.admission.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.util.FileUtils;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/base")
public class IndexAction extends CrudActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7409049479707603245L;

	private String id;

	public String visitorCount() throws IOException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		String r = "";
		path +="/other/visitCount.properties";
		File f = new File(path);
		if (!f.exists()) {
			f.createNewFile();
		}
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		if (session.get(id) == null) {
			r = FileUtils.getProperties(path, id, "1", true);
			session.put(id, "1");
		} else {
			r = FileUtils.getProperties(path, id, "1", false);
		}
		Struts2Utils.renderText(r);
		return null;
	}

	public String visitorClear() throws IOException {
		String path = this.getClass().getClassLoader().getResource("/")
				.getPath();
		path +="../other/visitCount.properties";
		File f = new File(path);
		if (f.exists()) {
			f.createNewFile();
		}
		id = "visitor-today";
		FileInputStream in;
		try {
			in = new FileInputStream(path);
			Properties p = new Properties();
			p.load(in);
			in.close();
			p.setProperty(id, "1");
			FileOutputStream out = new FileOutputStream(path);
			p.store(out, "visit update!");
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Object getModel() {
		return null;
	}

	@Override
	public String list() throws Exception {
		return null;
	}

	@Override
	public String input() throws Exception {
		return null;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}